<template>
  <view class="index-nav">
    <view
      class="item"
      v-for="(item, index) in indexNav"
      :key="index"
      hover-class="none"
      @click="goPage(item)"
    >
      <view class="icon">
        <image class="img" :src="item.icon" mode=""></image>
      </view>
      <view class="text">{{ item.name }}</view>
    </view>
		<!-- 授权 -->
		<view class="empower-flex">
			<l-popup :width="'90%'" :show="showEmpower" :modeType="'center'" @closeFn="closeEmp">
				<l-empower @backEmp="backEmp"></l-empower>
			</l-popup>
		</view>
		<!-- 身份选择 -->
		<view class="user-type-select">
		  <l-popup
		    :width="'90%'"
		    :show="showUserType"
		    :closeable="true"
		    :ismaskClose="true"
		    :modeType="'center'"
		    @closeFn="closeFn"
		  >
		    <l-user-type @roleType="roleType"></l-user-type>
		  </l-popup>
		</view>
  </view>
</template>

<script>
let app = getApp().globalData;
export default {
  name: "l-nav",
  props: {
    setTel: Boolean,
  },
  data() {
    return {
      indexNav: [
        {
          name: "发布工作",
          icon: "/static/img/navIcn1.png",
          path: "/pages/comA/published-work/published-work",
          type: 1,
        },
        {
          name: "我的报名",
          icon: "/static/img/navIcn2.png",
          path: "/pages/comA/my-registration/my-registration",
          type: 2,
        },
        {
          name: "我的招工",
          icon: "/static/img/navIcn3.png",
          path: "/pages/comA/my-recruit-workers/my-recruit-workers",
          type: 3,
        },
				// {
				//   name: "云南招聘网",
				//   icon: "/static/img/navIcn5.png",
				//   path: "",
				//   type: 5,
				// },
      ],
			showEmpower:false,
			showUserType:false,
			isauth: false,
			isEnt:false,
			isEntAuth: false,
			entStatus: {},
			userType: -1,
			checkItem: {},
			isLogin:false
    };
  },
  methods: {
		// 身份选择返回
		roleType(e) {
		  this.userType = e;
		  this.showUserType = false;
		  // 判断个人是否实名,判断企业是否认证，企业是否审核通过
		  if (this.userType == 0) {
		    // 个人
		    if (this.isauth) {
		      uni.$_navFn("to", this.checkItem.path);
		    } else {
		      uni.$_publicModal(
		        "个人身份发布工作需要进行实名认证，是否前往？",
		        function () {
		          const u = "/pages/comC/user-auth/user-auth";
		          uni.$_navFn("to", u);
		        }
		      );
		    }
		  } else {
		    // 企业
		    if (this.isEnt&&this.isEntAuth) {
		      // 判断企业是否认证
		      if (this.entStatus.status != 11) {
		        let tips =
		          "您的企业信息" +
		          this.entStatus.statusText +
		          ",如需加急,请联系客服!";
		        uni.$_timeOut(this, tips);
		      } else {
		        uni.$_navFn("to", this.checkItem.path);
		      }
		    }else{
					uni.$_publicModal(
					  "企业身份发布工作需要注册企业信息，是否前往？",
					  function () {
					    const u = "/pages/comB/ent-info/ent-info";
					    uni.$_navFn("to", u);
					  }
					);
				}
		  }
		},
		closeFn() {
		  this.showUserType = false;
		},
		// 检查用户是否认证
		isAuth() {
		  let that = this;
		  uni.$_get({
		    url: "is-auth",
		    that: that,
		    needlogin: true,
		    success: (res) => {
		      that.isauth = res.data;
		    },
		    fail: (err) => {
		      // uni.$_timeOut(that,err.message)
		    },
		  });
		},
		//判断当前用户关联的企业是否存在
		getIsExist(){
			let that = this
			uni.$_post({
			  url: "is-exist",
			  that: that,
			  needlogin: true,
			  success: (res) => {
					// console.log(res.data,'是否是企业'); 
					that.isEnt = res.data;
					if(res.data){
						that.getEntinfo()
					}
			  },
			  fail: (err) => {
			    // uni.$_timeOut(that,err.message)
			  },
			});
		},
		// 检查企业是否认证
		isEntAuthFn() {
		  let that = this;
		  uni.$_get({
		    url: "org-has-lic",
		    that: that,
		    needlogin: true,
		    success: (res) => {
		      that.isEntAuth = res.data;
		    },
		    fail: (err) => {
		      // uni.$_timeOut(that,err.message)
		    },
		  });
		},
		//得到当前用户的企业信息
		getEntinfo(){
			let that = this
			uni.$_get({
			  url: "get-ent-info",
			  that: that,
			  needlogin: true,
			  success: (res) => {
					that.entStatus = res.data;
			  },
			  fail: (err) => {
			    // uni.$_timeOut(that,err.message)
			  },
			});
		},
    goPage(e) {
			if (!uni.$_getToken()) {
				this.showEmp();
			}else if(uni.$_getToken()&&e.path != ""&&e.name=='发布工作'){
				this.checkItem = e;
				this.showUserType = true;
			}
			else{
				uni.$_navFn("to", e.path);
			}
      // this.$emit("userMenu", e);
    },
		// 授权登录
		backEmp(e) {
			let that = this
			if (e != undefined) {
				uni.$_loginWxMpPhone(e, this, "", app,function(){
					that.isAuth()
					that.getIsExist()
					that.isEntAuthFn()
					// that.getEntinfo()
				});
			}
			this.closeEmp()
		},
		// 开启授权弹窗
		showEmp() {
			this.showEmpower = true;
		},
		// 关闭授权弹窗
		closeEmp() {
			this.showEmpower = false;
		},
  },
  mounted() {
		if(uni.$_getToken()!=''){
			this.isAuth()
			this.getIsExist()
			this.isEntAuthFn()
		}
		
	},
  watch: {
		userType(v){
			this.userType = v
		},
		isEnt(v) {
		  this.isEnt = v;
		},
		isauth(v) {
		  this.isauth = v;
		},
		isEntAuth(v) {
		  this.isEntAuth = v;
		},
  },
};
</script>

<style lang="scss">
.index-nav {
  display: flex;
  justify-content: space-between;
  // align-items: center;
  padding: 20rpx 0 10rpx 0;
  .item {
    flex: 1;
    text-align: center;
    .icon {
      margin: 0 auto;
      .img {
        width: 78rpx;
        height: 96rpx;
      }
    }
    .text {
      font-size: $uni-font-size-28;
      color: #272829;
      font-weight: 500;
      margin-top: -8rpx;
    }
  }
}
</style>